package com.taptech.doufu.util;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.taptech.doufu.R;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.constant.SocialType;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.SinaWeiboActivity;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.bean.ShareBeansInfo;
import com.taptech.doufu.services.personalcenter.QQService;
import com.taptech.doufu.constant.ShareType;
import com.douhuayuedu.douhua.wxapi.WXEntryActivity;
import com.douhuayuedu.douhua.wxapi.WeixinService;

/**
 * Created by Wyatt on 2018/1/26/026.
 */

public class ShareUtils {

    public static BottomSheetDialog getBottomDialog(final Context context, final ShareBeansInfo shareBean) {
        final BottomSheetDialog dialog = new BottomSheetDialog(context);
        dialog.setContentView(R.layout.bottom_dialog_share_layout);

        dialog.findViewById(R.id.layCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });
        dialog.findViewById(R.id.layWX).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareBean.social_type = ShareType.Weixin;
                ShareUtils.thirdPartyShare(context,shareBean);
                dialog.cancel();
            }
        });
        dialog.findViewById(R.id.layPYQ).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareBean.social_type = ShareType.pengyouquan;
                ShareUtils.thirdPartyShare(context,shareBean);
                dialog.cancel();
            }
        });
        dialog.findViewById(R.id.layQQ).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareBean.social_type = ShareType.QQ;
                ShareUtils.thirdPartyShare(context,shareBean);
                dialog.cancel();
            }
        });
        dialog.findViewById(R.id.layQQZone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareBean.social_type = ShareType.QQSpace;
                ShareUtils.thirdPartyShare(context,shareBean);
                dialog.cancel();
            }
        });

        dialog.findViewById(R.id.layWeibo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shareBean.social_type = ShareType.Weibo;
                ShareUtils.thirdPartyShare(context,shareBean);
                dialog.cancel();
            }
        });

        dialog.findViewById(R.id.layLink).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                copyLike(context,shareBean.shareUrl);
                dialog.cancel();
            }
        });
        return dialog;
    }

    public interface OnClickListener {
        void onClickItem(int social_type);

        void onClickHelp();
    }

    public static BottomSheetDialog getBottomDialog(final Context context,boolean showHelpLayout, final OnClickListener onClickListener) {
        final BottomSheetDialog dialog = new BottomSheetDialog(context);
        dialog.setContentView(R.layout.bottom_dialog_share_layout_with_poster);
        View lay = dialog.findViewById(R.id.design_bottom_sheet);
        if (lay != null) {
            lay.setBackgroundColor(context.getResources().getColor(R.color.transparent_background));
        }
        LinearLayout helpLayout = dialog.findViewById(R.id.layHelp);
        if (showHelpLayout) {
            helpLayout.setVisibility(View.VISIBLE);
            helpLayout.setOnClickListener(v -> onClickListener.onClickHelp());
        }else{
            helpLayout.setVisibility(View.GONE);
        }

        dialog.findViewById(R.id.layCancel).setOnClickListener(v -> dialog.cancel());
        dialog.findViewById(R.id.layWX).setOnClickListener(v -> {
            onClickListener.onClickItem(ShareType.Weixin);
            dialog.cancel();
        });
        dialog.findViewById(R.id.tvDetail).setOnClickListener(v -> {
            BrowseActivity.startActivity(context,"https://api.doufu.la/activity/inviteNew");
        });
        dialog.findViewById(R.id.layPYQ).setOnClickListener(v -> {
            onClickListener.onClickItem(ShareType.pengyouquan);
            dialog.cancel();
        });
        dialog.findViewById(R.id.layQQ).setOnClickListener(v -> {
            onClickListener.onClickItem(ShareType.QQ);
            dialog.cancel();
        });
        dialog.findViewById(R.id.layQQZone).setOnClickListener(v -> {
            onClickListener.onClickItem(ShareType.QQSpace);
            dialog.cancel();
        });

        dialog.findViewById(R.id.layWeibo).setOnClickListener(v -> {
            onClickListener.onClickItem(ShareType.Weibo);
            dialog.cancel();
        });

        dialog.findViewById(R.id.layLink).setOnClickListener(v -> {
            onClickListener.onClickItem(ShareType.CopyLink);
            dialog.cancel();
        });
        dialog.findViewById(R.id.layMakePoster).setOnClickListener(v -> {
            onClickListener.onClickItem(ShareType.Poster);
            dialog.cancel();
        });
        return dialog;
    }

    public static void copyLike(Context context,String url) {
        //复制链接
        ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText("share", url);
        clipboardManager.setPrimaryClip(clipData);
        Toast.makeText(WeMediaApplication.applicationContext, "复制成功", Toast.LENGTH_LONG).show();
    }


    public static void thirdPartyShare(Context context, ShareBeansInfo info) {
        if (info == null) {
            Toast.makeText(WeMediaApplication.applicationContext, "操作失败", Toast.LENGTH_LONG).show();
            return;
        }
//        String desc = "我正在 豆花阅读 App 看《"+info.title+"》小说，99%女生都在用的耽美言情BL同人小说阅读器，邀请你跟我一起看!!! "+info.shareUrl;
//        ShareUtil.Companion.shareText(context,info.title,desc);


        if (info.social_type == ShareType.Weixin) {
            info.setSocial_type(SocialType.WEIXIN);
            WXEntryActivity.Companion.setShareInfo(info);
            if (!TextUtils.isEmpty(info.getImagUrl())) {
                WeixinService.getInstance(context).shareToWeixin(info);
            }else{
                WeixinService.getInstance(context).shareToWeixinDefaulIcon(info);
            }
        }else if(info.social_type == ShareType.pengyouquan){
            WXEntryActivity.Companion.setShareInfo(info);
            info.setSocial_type(SocialType.PENGYOUQUAN);
            if (!TextUtils.isEmpty(info.getImagUrl())) {
                WeixinService.getInstance(context).shareToPengyouquan(info);
            }else{
                WeixinService.getInstance(context).shareToPengyouquanDefaulIcon(info);
            }
        } else if (info.social_type == ShareType.QQ) {
            info.setSocial_type(SocialType.QQ);
            QQService.getInstance().shareToQQ((Activity) context,info);
        } else if (info.social_type == ShareType.QQSpace) {
            info.setSocial_type(SocialType.QQSPACE);
            QQService.getInstance().shareQQSpace((Activity) context,info);
        } else if (info.social_type == ShareType.Weibo) {
            info.setSocial_type(SocialType.WEIBO);
            info.setFlag(ShareBeansInfo.NOVEL_SHARE_FLAG);
            SinaWeiboActivity.shareToWeibo(context,info);
        }else{
            Toast.makeText(WeMediaApplication.applicationContext, "操作失败", Toast.LENGTH_LONG).show();
        }
    }

    public static void thirdPartyShare(Context context,String data,boolean isForWeex) {
        try {
            JSONObject jsonObject = JSON.parseObject(data);
            ShareBeansInfo info = new ShareBeansInfo();
            if (jsonObject.containsKey("dataType")) { //分享的数据类型 普通分享：1 ；仅分享图片：2
                info.setDataType(jsonObject.getInteger("dataType"));
            }
            int type = jsonObject.getInteger("type");  //分享渠道类型 微博：1  微信好友：2  QQ：3  QQ空间：4   朋友圈： 5
            info.setSocial_type(type);
            if (jsonObject.containsKey("desc")) {  //描述
                info.setShartText(jsonObject.getString("desc"));
            }
            if (jsonObject.containsKey("cover")) {  //封面
                info.setImagUrl(jsonObject.getString("cover"));
            }
            if (jsonObject.containsKey("shareUrl")) {  //跳转链接
                info.setShareUrl(jsonObject.getString("shareUrl"));
            }
            if (jsonObject.containsKey("title")) {  //标题
                info.setTitle(jsonObject.getString("title"));
            }
            if (jsonObject.containsKey("id")) {  //
                info.setId(jsonObject.getString("id"));
            }
            if (jsonObject.containsKey("objectType")) { //
                info.setObject_type(jsonObject.getString("objectType"));
            }
            info.setForWeex(isForWeex);
            thirdPartyShare(context,info);
        } catch (Exception e) {
            Toast.makeText(WeMediaApplication.applicationContext, "操作失败", Toast.LENGTH_LONG).show();
        }
    }

    public static void shareBigPic(Context context, int socialType, Bitmap bitmap,ShareBeansInfo info) {
        if (bitmap == null) {
            Toast.makeText(context,"图片异常，分享失败",Toast.LENGTH_LONG).show();
            return;
        }
        if (socialType == ShareType.Weixin) {
            WeixinService.getInstance(context).shareBigPicToWeixin(bitmap);
        } else if (socialType == ShareType.pengyouquan) {
            WeixinService.getInstance(context).shareBigPicToPengyouquan(bitmap);
        } else if (socialType == ShareType.QQ) {
            QQService.getInstance().shareToQQ((Activity) context,info);
        } else if (socialType == ShareType.QQSpace) {
            QQService.getInstance().shareQQSpace((Activity) context,info);
        } else if (socialType == ShareType.Weibo) {
            SinaWeiboActivity.shareToWeibo(context,info);
        }
    }

    public static void shareBigPic(Context context, int socialType, Bitmap bitmap) {
        if (bitmap == null) {
            Toast.makeText(context,"图片异常，分享失败",Toast.LENGTH_LONG).show();
            return;
        }
        if (socialType == ShareType.Weixin) {
            WeixinService.getInstance(context).shareBigPicToWeixin(bitmap);
        } else if (socialType == ShareType.pengyouquan) {
            WeixinService.getInstance(context).shareBigPicToPengyouquan(bitmap);
        } else if (socialType == ShareType.QQ) {
            ShareBeansInfo info = new ShareBeansInfo();
            info.setSocial_type(socialType);
            info.setDataType(Constant.ShareDataType.SHARE_DATA_TYPE_IMAGE);
            String tempPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/poster.png";
            BitmapUtil.saveSDcard(bitmap, tempPath);
            info.setImagUrl(tempPath);
            QQService.getInstance().shareToQQ((Activity) context,info);
        } else if (socialType == ShareType.QQSpace) {
            ShareBeansInfo info = new ShareBeansInfo();
            info.setSocial_type(socialType);
            info.setDataType(Constant.ShareDataType.SHARE_DATA_TYPE_IMAGE);
            String tempPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/poster.png";
            BitmapUtil.saveSDcard(bitmap, tempPath);
            info.setImagUrl(tempPath);
            QQService.getInstance().shareQQSpace((Activity) context,info);
        } else if (socialType == ShareType.Weibo) {
            ShareBeansInfo info = new ShareBeansInfo();
            info.setSocial_type(socialType);
            info.setFlag(ShareBeansInfo.NOVEL_SHARE_FLAG);
            String tempPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/temp_poster_"+System.currentTimeMillis()+".png";
            BitmapUtil.saveSDcard(bitmap, tempPath);
            info.setImagUrl(tempPath);
            SinaWeiboActivity.shareToWeibo(context,info);
        }
    }
}
